package com.anolesoft.amljmes.fortyone.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.anolesoft.amljmes.fortyone.dto.MesOrderTraceDto;
import com.anolesoft.amljmes.fortyone.dto.MesStoveBaseDto;
import com.anolesoft.amljmes.fortyone.dto.MesStoveFabinfoDto;
import com.anolesoft.amljmes.fortyone.service.MesStoveBaseService;
import com.anolesoft.amljmes.fortyone.service.MesStoveFabinfoService;
import com.anolesoft.amljmes.test.fenguangyingdu.dto.FenguangYingduEmpDTO;
import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.common.security.util.SecurityUtils;
import com.anolesoft.yr.util.Constants;

import lombok.extern.slf4j.Slf4j;

@RestController
@CrossOrigin
@RequestMapping(value="/mesStoveFabinfo")
@Slf4j
public class MesStoveFabinfoController {
    @Autowired
    MesStoveFabinfoService mesStoveFabinfoService;
    @Autowired
    MesStoveBaseService mesStoveBaseService;

    @PostMapping(value="/all",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult MesStoveFabinfoAll(@RequestBody Map<String, String> map){
        String StoveFabinfoObj = map.get("objuid");
        List<MesStoveFabinfoDto> list = mesStoveFabinfoService.MesStoveFabinfoAll(StoveFabinfoObj);
        AjaxResult success = AjaxResult.success();
        success.put("data", list);
        return success;
    }

    /**
     * 林雪
     * 根据41厂制造记录条码查询
     * @param map
     * @return
     */
    @PostMapping(value="/queryByZzjl",produces= MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult MesStoveFabinfoByZzjl(@RequestBody Map<String, String> map) {
        String fabrecordid = map.get("fabrecordid");
        System.out.println("fabrecordid"+fabrecordid);
        MesOrderTraceDto mesOrderTraceDto=mesStoveFabinfoService.MesStoveFabinfoByZzjl(fabrecordid);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesOrderTraceDto);
    }

    //删除选中按钮
    @PostMapping(value="/updateByDelete", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult updateMesStoveFabinfoDelete(@RequestBody  String[] objuids){
        System.out.println(">>>>>>updateDelete");
        List<String> asList = null;
        if (objuids != null) {
            asList = Arrays.asList(objuids);
        }
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,
                mesStoveFabinfoService.updateMesStoveFabinfoDelete(asList));
    }


    //取消选中按钮
    @PostMapping(value="/updateByCancel", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult updateMesStoveFabinfoCancel(@RequestBody  String[] objuids){
        System.out.println(">>>>>>updateCancel");
        List<String> asList = null;
        if (objuids != null) {
            asList = Arrays.asList(objuids);
        }
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,
                mesStoveFabinfoService.updateMesStoveFabinfoCancel(asList));
    }

    /**
     * 林雪
     * 修改数量
     * @param queryInfoDto
     * @return
     */
//    @PostMapping(value="/updateBySl", produces = MediaType.APPLICATION_JSON_VALUE)
//    @ResponseBody
//    public AjaxResult updateMesStoveFabinfoSl(@RequestBody  List<MesStoveFabinfoDto> queryInfoDto){
//        System.out.println(">>>>>>updateCancel");
//        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesStoveFabinfoService.updateMesStoveFabinfoSl(queryInfoDto));
//    }

    /**
     * 林雪
     * 根据炉次编号查工序名
     * @param map
     * @return
     */
    @PostMapping(value="/queryByGxname",produces= MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult MesStoveFabinfoByGxname(@RequestBody Map<String, String> map) {
        String StoveFabinfoObj = map.get("objuid");
        MesStoveFabinfoDto mesStoveFabinfoDto=mesStoveFabinfoService.MesStoveFabinfoByGxname(StoveFabinfoObj);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesStoveFabinfoDto);
    }

    /**
     * 林雪
     * 根据当前登录人查胸卡号密码
     */
    @PostMapping(value="/queryEmpPassword",produces= MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult queryEmpPassword() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String obj=loginUser.getEmpUid();
        System.out.println("obj"+obj);
        FenguangYingduEmpDTO fenguangYingduEmpDTO=mesStoveFabinfoService.queryEmpPassword(obj);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,fenguangYingduEmpDTO);
    }

    /**
     * 林雪
     * 根据当前登录人查password2
     */
    @PostMapping(value="/queryEmpPassword2",produces= MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult queryEmpPassword2() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String obj=loginUser.getEmpUid();
        System.out.println("obj"+obj);
        FenguangYingduEmpDTO fenguangYingduEmpDTO=mesStoveFabinfoService.queryEmpPassword2(obj);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,fenguangYingduEmpDTO);
    }

    //根据工序名包含入厂查询质量信息
    @PostMapping(value="/rc",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult MesStoveFabinfoByRc(){
        List<MesStoveFabinfoDto> list = mesStoveFabinfoService.MesStoveFabinfoByRc();
        AjaxResult success = AjaxResult.success();
        success.put("data", list);
        return success;
    }

    //根据工序名包含清洗查询质量信息
    @PostMapping(value="/qx",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult MesStoveFabinfoByQx(){
        List<MesStoveFabinfoDto> list = mesStoveFabinfoService.MesStoveFabinfoByQx();
        AjaxResult success = AjaxResult.success();
        success.put("data", list);
        return success;
    }

    //根据工序名包含干燥查询质量信息
    @PostMapping(value="/gz",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult MesStoveFabinfoByGz(){
        List<MesStoveFabinfoDto> list = mesStoveFabinfoService.MesStoveFabinfoByGz();
        AjaxResult success = AjaxResult.success();
        success.put("data", list);
        return success;
    }

    //根据工序名包含脆性查询质量信息
    @PostMapping(value="/cx",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult MesStoveFabinfoByCx(){
        List<MesStoveFabinfoDto> list = mesStoveFabinfoService.MesStoveFabinfoByCx();
        AjaxResult success = AjaxResult.success();
        success.put("data", list);
        return success;
    }

    //根据工序名包含硬度检查查询质量信息
    @PostMapping(value="/ydjc",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult MesStoveFabinfoByYdjc(){
        List<MesStoveFabinfoDto> list = mesStoveFabinfoService.MesStoveFabinfoByYdjc();
        AjaxResult success = AjaxResult.success();
        success.put("data", list);
        return success;
    }

    //根据工序名包含校正查询质量信息
    @PostMapping(value="/jz",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult MesStoveFabinfoByJz(){
        List<MesStoveFabinfoDto> list = mesStoveFabinfoService.MesStoveFabinfoByJz();
        AjaxResult success = AjaxResult.success();
        success.put("data", list);
        return success;
    }

    //根据工序名包含检验查询质量信息
    @PostMapping(value="/jy",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult MesStoveFabinfoByJy(){
        List<MesStoveFabinfoDto> list = mesStoveFabinfoService.MesStoveFabinfoByJy();
        AjaxResult success = AjaxResult.success();
        success.put("data", list);
        return success;
    }

    //根据工序名包含碳不包含氮查询质量信息
    @PostMapping(value="/td",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult MesStoveFabinfoByTd(){
        List<MesStoveFabinfoDto> list = mesStoveFabinfoService.MesStoveFabinfoByTd();
        AjaxResult success = AjaxResult.success();
        success.put("data", list);
        return success;
    }

    //根据工序名包含氮查询质量信息
    @PostMapping(value="/d",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult MesStoveFabinfoByD(){
        List<MesStoveFabinfoDto> list = mesStoveFabinfoService.MesStoveFabinfoByD();
        AjaxResult success = AjaxResult.success();
        success.put("data", list);
        return success;
    }

    //根据工序名包含火或为预热或固溶或冷处理或氧化处理或奥式体化或时效或沉淀硬化查询质量信息
    @PostMapping(value="/hor",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult MesStoveFabinfoByHor(){
        List<MesStoveFabinfoDto> list = mesStoveFabinfoService.MesStoveFabinfoByHor();
        AjaxResult success = AjaxResult.success();
        success.put("data", list);
        return success;
    }

    //新增
    @PostMapping(value="/add", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult addMesStoveBase(@RequestBody MesStoveBaseDto queryInfoDto){
        System.out.println(">>>>>>add");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        queryInfoDto.setCjr(loginUser.getEmpUid());
        queryInfoDto.setFactoryId(loginUser.getFacCode());
        queryInfoDto.setPgr(loginUser.getEmpUid());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, mesStoveBaseService.addMesStoveBase(queryInfoDto));
    }

    //炉次管理-保存
    @PostMapping(value = "/save", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult insertMesStoveFabinfo(@RequestBody List<MesStoveFabinfoDto> amStoveFabinfoDto){
        System.out.print(">>>>>>>>>>>>>>>>"+amStoveFabinfoDto.toString());
        for (MesStoveFabinfoDto s : amStoveFabinfoDto) {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            s.setCjr(loginUser.getEmpUid());
            s.setFactoryId(loginUser.getFacCode());
        }
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE,mesStoveFabinfoService.insertMesStoveFabinfo(amStoveFabinfoDto));
    }


    //41厂工序名是否包含工序名关键字符
    @PostMapping(value="/like", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult liketMesStoveFabinfo(@RequestParam("obj1") String obj1,@RequestParam("obj2") String obj2){
        String objuid = obj1;
        String fabrecordid = obj2;
        String fortyoneCodes = "";
        int count = mesStoveFabinfoService.liketMesStoveFabinfo(objuid, fabrecordid);
        if (count == 0) {
            fortyoneCodes="该产品制造记录条码当前工序名不包含工序名关键字符!";
        }
        return new AjaxResult(HttpStatus.SUCCESS, fortyoneCodes);
    }

    //获取41厂制造记录、当前工序名、当前工序号
    @PostMapping(value="/queryBy41",produces= MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult MesStoveFabinfoBy41(@RequestBody Map<String, String> map) {
        String StoveFabinfoObj = map.get("objuid");
        List<MesStoveFabinfoDto> list = mesStoveFabinfoService.MesStoveFabinfoBy41(StoveFabinfoObj);
        AjaxResult success = AjaxResult.success();
        success.put("data", list);
        return success;
    }

    //根据炉次编号查询所在炉次内的制造记录有哪些
    @PostMapping(value="/queryZzjl",produces= MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult mesStoveFabinfoZzjl(@RequestParam("stoveFabinfoObj") String stoveFabinfoObj) {
        List<MesOrderTraceDto> MesOrderTraceDto=mesStoveFabinfoService.mesStoveFabinfoZzjl(stoveFabinfoObj);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,MesOrderTraceDto);
    }
    
    //根据炉次编号查询所在炉次内的标记有哪些
    @PostMapping(value="/queryBiaoji",produces= MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult mesStoveFabinfobiaoji(@RequestParam("stoveFabinfoObj") String stoveFabinfoObj) {
        List<MesStoveFabinfoDto> MesStoveFabinfoDto=mesStoveFabinfoService.mesStoveFabinfobiaoji(stoveFabinfoObj);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,MesStoveFabinfoDto);
    }

    @PutMapping(value="/aaa/{lcid}",produces= MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult update(@PathVariable("lcid")String lcid){
        mesStoveFabinfoService.updateFabinfo(lcid,SecurityUtils.getLoginUser().getEmpUid(),SecurityUtils.getLoginUser().getUsername());
        return AjaxResult.success();
    }
}
